﻿//Author: Tran Quang Dang Khoa
// Group : 06TH1d.12
// Created : 12/5/2010
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Windows.Forms;

namespace QUANLYNHASACH.App_code.BLL
{
    public class THELOAI_BLL
    {
        #region LAY DANH SACH THE LOAI SACH
        public List<DAL.THELOAI_td> LayDanhSachTheLoai()
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(QUANLYNHASACH.connect.GetChuoiKetNoi()))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "getTheLoaiSach";
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlDataReader reader = cmd.ExecuteReader();

                    List<DAL.THELOAI_td> theloaiLST = new List<DAL.THELOAI_td>();

                    while (reader.Read())
                    {

                        DAL.THELOAI_td theloai = new DAL.THELOAI_td();

                        theloai.idTheLoai = reader[0].ToString();

                        theloai.TenTheLoai = reader[1].ToString();
                        theloaiLST.Add(theloai);

                    }
                    conn.Close();
                    return theloaiLST;
                };


            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi xảy ra: " + ex.Message, "Thông báo lỗi");
            }
            return null;
        }
        #endregion

        #region THEM THE LOAI
        public void ThemTheLoai(string idTheLoai, string TenTheLoai)
        {
           // bool result = false;
            try {
                using (SqlConnection conn = new SqlConnection(QUANLYNHASACH.connect.GetChuoiKetNoi()))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "addTheLoaiSach";
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    DbParameter param = cmd.CreateParameter();
                    param.ParameterName = "@idTheLoai";
                    param.Value = idTheLoai;
                    param.DbType = DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@TenTheLoai";
                    param.Value = TenTheLoai;
                    param.DbType = DbType.String;
                    cmd.Parameters.Add(param);

                    cmd.ExecuteNonQuery();
                    result = true;
                    conn.Close();
                };
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message, "Thông báo lỗi");
              //  result = false;
            }
           // return result;
        }
        #endregion

        #region SUA THE LOAI
        public void suaTheLoai(string idTheLoai, string TenTheLoai_new)
        {
           // bool kq = false;
            try
            {
                using (SqlConnection conn = new SqlConnection(QUANLYNHASACH.connect.GetChuoiKetNoi()))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "editTheLoaiSach";
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    DbParameter param = cmd.CreateParameter();
                    param.ParameterName = "@idTheLoai";
                    param.Value = idTheLoai;
                    param.DbType = DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@TenTheLoai_new";
                    param.Value = TenTheLoai_new;
                    param.DbType = DbType.String;
                    cmd.Parameters.Add(param);

                    cmd.ExecuteNonQuery();

                   // kq = true;
                    conn.Close();
                };
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message, "Thông báo lỗi");
              //  kq = false;
            }
         //   return kq;
        }
        #endregion

        #region XOA THE LOAI
        public void xoaTheLoai(string idTheLoai)
        {
            //bool ketqua = false;
            try
            {
                using (SqlConnection conn = new SqlConnection(QUANLYNHASACH.connect.GetChuoiKetNoi()))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delTheLoaiSach";
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    DbParameter param = cmd.CreateParameter();
                    param.ParameterName = "@idTheLoai";
                    param.Value = idTheLoai;
                    param.DbType = DbType.String;
                    cmd.Parameters.Add(param);

                    cmd.ExecuteNonQuery();
                  //  ketqua = true;
                    conn.Close();
                };
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message, "Thông báo lỗi");
              //  ketqua = false;
            }
           // return ketqua;
        }
        #endregion
    }
}
